package com.thinking.project.monitorserver.controller;

import com.thinking.project.monitorserver.pojo.Worker;
import com.thinking.project.monitorserver.service.WorkerService;
import com.thinking.project.monitorserver.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Description 功能描述：  员工信息表
 * @Author tangsixiang@163.com
 * @DATE 2019/6/21 11:21
 * @Version 1.0
 **/

@RestController
@RequestMapping(value = "/worker")
public class WorkerController {

    @Autowired
    StringRedisTemplate template;

    @Autowired
    WorkerService workerService;

    /**
     * @Author tangsixiang@163.com 
     * @Description //TODO  采用官方提供参加校验方式 
     * @Date   14:10 2019/11/25  
     * @Param [worker, result] 
     * @return com.thinking.project.monitorserver.util.ResponseData
     **/
    @RequestMapping(value = "/addWorker", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseData addWorker(@RequestBody @Validated Worker worker, BindingResult result) {

        if (result.getFieldErrorCount() >= 1) {
            return ResponseData.badRequest().putDataValue("errorMessage", result.getFieldErrors());
        } else {
            int saveNum = workerService.insertSelective(worker);
            if (saveNum == 1) {
                return new ResponseData(saveNum, "保存成功").putDataValue("worker",worker);
            } else {
                return new ResponseData(saveNum, "保存异常").putDataValue("worker",worker);
            }
        }

    }

    @RequestMapping(value = "/updateWorker", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseData updateWorkder(@RequestBody @Validated Worker worker, BindingResult result) {

        if (result.getFieldErrorCount() >= 1) {
            return ResponseData.badRequest().putDataValue("errorMessage", result.getFieldErrors());
        } else {
            int saveNum = workerService.updateByPrimaryKeySelective(worker);
            if (saveNum == 1) {
                return new ResponseData(saveNum, "修改成功");
            } else {
                return new ResponseData(saveNum, "修改异常");
            }
        }
    }

    @RequestMapping(value = "/delWorker", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseData delWorker(@RequestBody Worker worker) {
        if (worker.getWorkerId() >= 1) {
            int saveNum = workerService.deleteByPrimaryKey(worker.getWorkerId());
            if (saveNum == 1) {
                return new ResponseData(saveNum, "删除成功");
            } else {
                return new ResponseData(saveNum, "删除异常");
            }

        } else {
            return ResponseData.badRequest().putDataValue("message", "员工信息主键必须 workerId不能为空！");
        }
    }

    @RequestMapping(value = "/queryWorkerPage", method = {RequestMethod.POST, RequestMethod.GET})

    public ResponseData queryWorkder(@RequestBody Map<String, Object> map) {
        String  wokererName=null;
              if(map.containsKey("workerName")){
                  wokererName=map.get("workerName")==null?null:map.get("workerName").toString();
              }

        int count = workerService.selectForListNum(wokererName);
        if (count >= 1) {
            List<Worker> list = workerService.selectForListPageNoPassword(wokererName, map.getOrDefault("startPage", 1), map.getOrDefault("pageSize", 10));
            return ResponseData.ok().putDataValue("count", count).putDataValue("list", list);
        } else {
            return ResponseData.ok().putDataValue("count", count).putDataValue("list", null);
        }
    }

//    @RequestMapping(value = "/login",method = {RequestMethod.POST,RequestMethod.GET})
//    public ResponseData loginWorkder(@RequestBody Worker worker) {
//        Worker user = workerService.selectByUserName(worker);
//        if (user == null) {
//            return new ResponseData(0,"无对应账号，请核实账号!" );
//        } else if (!user.getPassword().equals(worker.getPassword())) {
//            return new ResponseData(-1,"密码错误！");
//        } else {
//            try {
//                template.opsForValue().set(String.format("string:login:userId:[%s]", worker.getWorkerId()), new ObjectMapper().writeValueAsString(user), 30, TimeUnit.MINUTES);
//                return new ResponseData( 1,"登录成功！" );
//            } catch (JsonProcessingException e) {
//                e.printStackTrace();
//                return new ResponseData(-1,"登录异常，系统错误！" );
//            }
//        }
//
//    }
//
//    @RequestMapping(value = "/logout", method = {RequestMethod.POST,RequestMethod.GET})
//    public ResponseData logoutWorkder(@RequestBody Worker worker) {
//        template.opsForValue().decrement(String.format("string:login:userId:[%s]", worker.getWorkerId()));
//        return ResponseData.ok();
//    }


}
